export interface ScheduleItem {
  id: string
  title: string
  date: string
  notes?: string
}

const KEY = 'schedules_v1'

export function getSchedules(): ScheduleItem[] {
  try {
    const raw = localStorage.getItem(KEY)
    if (!raw) return []
    return JSON.parse(raw)
  } catch (e) { return [] }
}

export function saveSchedules(list: ScheduleItem[]) {
  localStorage.setItem(KEY, JSON.stringify(list))
}

export function addSchedule(item: ScheduleItem) {
  const list = getSchedules()
  list.push(item)
  saveSchedules(list)
}

export function updateSchedule(id: string, patch: Partial<ScheduleItem>) {
  const list = getSchedules()
  const idx = list.findIndex(i => i.id === id)
  if (idx === -1) return false
  list[idx] = { ...list[idx], ...patch } as ScheduleItem
  saveSchedules(list)
  return true
}

export function deleteSchedule(id: string) {
  let list = getSchedules()
  list = list.filter(i => i.id !== id)
  saveSchedules(list)
}
